from django import http
import json

from django.shortcuts import render

from goods.models import SKU
from meiduo_mall.utils.constants import Code
from meiduo_mall.utils.login_required import MyLoginRequest
from orders.models import OrderInfo
from django.views import View


class OrderCommentView(MyLoginRequest):
    def get(self, request):
        # 获取参数
        data_dict = request.GET
        order_id = data_dict.get('order_id')

        # 校验参数
        try:
            order = OrderInfo.objects.get(order_id=order_id)
        except:
            return http.HttpResponseForbidden('订单不存在')

        # 数据拼接
        skus = []

        for order_good in order.skus.all():
            sku = order_good.sku
            skus.append({
                'order_id': order_id,
                'img': sku.default_image_url.url,
                'name': sku.name,
                'price': str(sku.price),
                'sku_id': sku.id,
                'final_score': 0,
            })

        return render(request, 'goods_judge.html', context={'skus': skus})

    def post(self, request):

        # 获取参数
        data_dict = json.loads(request.body.decode())
        order_id = data_dict.get('order_id')
        sku_id = data_dict.get('sku_id')
        comment = data_dict.get('comment')
        score = data_dict.get('score')
        is_anonymous = data_dict.get('is_anonymous', False)

        # 校验参数
        if not all([order_id, sku_id, comment, score]):
            return http.JsonResponse({'code': Code.PARAMETER_ERROR, 'errmsg': '参数不全'})

        try:
            order = OrderInfo.objects.get(order_id=order_id)
        except:
            return http.JsonResponse({'code': Code.PARAMETER_ERROR, 'errmsg': '订单号不存在'})

        good_orders = order.skus.all()

        if not good_orders:
            return http.JsonResponse({'code': Code.PARAMETER_ERROR, 'errmsg': '商品不存在'})

        # 判段该订单是否所有商品都评论完成
        is_total_comment = True

        for good_order in good_orders:
            if good_order.sku_id == sku_id:
                good_order.is_anonymous = is_anonymous
                good_order.comment = comment
                good_order.score = score
                good_order.save()
            if not good_order.comment:
                is_total_comment = False

        if is_total_comment:
            order.status = OrderInfo.ORDER_STATUS_ENUM['FINISHED']
            order.save()

        return http.JsonResponse({'code': Code.OK})


class CommentView(View):

    def get(self, request, sku_id):
        if not sku_id:
            return http.JsonResponse({'error_msg': '参数不全'}, status=400)
        try:
            sku = SKU.objects.get(id=sku_id)
        except:
            return http.JsonResponse({'error_msg': '商品不存在'}, status=400)

        comments = []
        for comment in sku.ordergoods_set.all():
            if comment.is_anonymous:
                user_name = comment.order.user.username
            else:
                user_name = 'xxxx'
            comments.append({
                'score': comment.score,
                'comment': comment.comment,
                'is_anonymous': comment.is_anonymous,
                'user_name': user_name
            })

        return http.JsonResponse({'comments': comments})
